//
//  WPDateFormatterManager.m
//  wobingwoyi
//
//  Created by WGP on 16/8/11.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPDateFormatterManager.h"

@implementation WPDateFormatterManager

+(instancetype)sharedManager
{
    static WPDateFormatterManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[self alloc] init];
        
    });
    return instance;
}

-(NSString *)timeStrWithTimeStampStr:(NSString *)timeStampStr
{
    NSTimeInterval _interval=[timeStampStr doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [WPDateFormatterManager sharedManager];
    [objDateformat setDateFormat:@"yyyy-MM-dd"];
    
    return [objDateformat stringFromDate: date];

}

-(NSString *)timeStrWithTimeStampStr:(NSString *)timeStampStr formatStr:(NSString *)format
{
    NSTimeInterval _interval=[timeStampStr doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [WPDateFormatterManager sharedManager];
    [objDateformat setDateFormat:format];
    
    return [objDateformat stringFromDate: date];
    
}
-(NSString *)wbwyTimeStrDescriptionWithTimeStampStr:(NSString *)timeStampStr
{
    NSTimeInterval _interval=[timeStampStr doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
//    // 指定字符串的格式
//    self.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//    self.locale = [NSLocale localeWithLocaleIdentifier:@"EN"];
    NSCalendar *calender = [NSCalendar currentCalendar];
    if ([calender isDateInToday:date])
    {
        int delta = (int)([[NSDate date] timeIntervalSinceDate:date]);
        
        // 一分钟内
        if (delta < 60)
        {
            return @"刚刚";
        }
        
        // 一小时内
        if (delta < 60 * 60)
        {
            // X分钟前
            return [NSString stringWithFormat:@"%zd分钟前",delta/60];
        }
        
        // 当天
        // X小时前
        return [NSString stringWithFormat:@"%zd小时前",delta/60/60];
    }
    
    // 后面3中需要时间格式化
    NSString * dateFormat = @"";
    
    // 昨天
    if ([calender isDateInYesterday:date ])
    {
        dateFormat = @"昨天 HH:mm";
    }
    else if ([calender compareDate:date toDate:[NSDate date] toUnitGranularity:NSCalendarUnitYear] == NSOrderedSame)
    {
        // 一年内
        /// 比较时间, 新浪时间和当前时间比较, 相同
        /// date1: NSDate
        /// toDate: NSDate
        /// toUnitGranularity 比较的单位
        /// NSDate(): 系统当前时间
        dateFormat = @"MM-dd HH:mm";
    } else
    {

        dateFormat = @"yyyy-MM-dd HH:mm";
    }
    self.dateFormat = dateFormat;
    self.locale = [NSLocale localeWithLocaleIdentifier:@"EN"];
    
    NSString *dateStr = [self stringFromDate:date];
    return dateStr;
}

- (NSString *)stringFromDateWithDate:(NSDate *)date  format:(NSString *)format {
    // 设置时间格式
    self.dateFormat = format;
    // 转换字符串
    return [self stringFromDate:date];
}

-(NSDate *)dateFromString:(NSString *)timeStr format:(NSString *)format {
    // 设置时间格式
    self.dateFormat = format;
    // 设置datePicker当前显示的时间
    return [self dateFromString:timeStr];
}

@end
